//
//  HMListenViewController.m
//  HowToDo
//
//  Created by Apple on 16/12/31.
//  Copyright © 2016年 mynsoo. All rights reserved.
//

#import "HMListenViewController.h"
#import "HMAudioPlayerController.h"
#import "HMNetWorkTool.h"
#import "HMListenAnyTimeResViewModel.h"
#import "MOPopWindow.h"
#import "HMListenLoadAudioViewController.h"
#import "HMListenAnyTimeResViewModel.h"
#import "HMListenAnyTimeResModel.h"
#import "HMMusicImageView.h"

@interface HMListenViewController ()

/**
 用户界面部分
 */

///用户头像
@property (nonatomic, weak) UIImageView *userImageView;
///欢迎词
@property (nonatomic, weak) UILabel *welcomeLabel;
///分隔线
@property (nonatomic, weak) UIView *separatorView;

/**
 中部文本
 */

///主文本
@property (nonatomic, weak) UILabel *textLabel;
///子文本
@property (nonatomic, weak) UILabel *subTextLabel;

/**
 随时听按钮部分
 */

///随时听30分钟按钮
@property (nonatomic, weak) UIButton *dailyAudio30;
///随时听15分钟按钮
@property (nonatomic, weak) UIButton *dailyAudio15;
///随时听60分钟按钮
@property (nonatomic, weak) UIButton *dailyAudio60;
///随时听120分钟按钮
@property (nonatomic, weak) UIButton *dailyAudio120;

/**
 上一个播放按钮
 */
@property (nonatomic, weak) UIButton *lastPlayBtn;
@property (nonatomic, weak) UILabel *lastPlayLabel;
@property (nonatomic, weak) HMMusicImageView * iconImageView;

@end

@implementation HMListenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 设置导航栏
    [self installNavigationBar];
    // 设置用户界面部分
    [self installUserImageView];
    [self installTextLabels];
    // 设置随时听按钮
    [self installDailyAudioBtn];
    // 设置播放
    [self installLastPlayBtn];
}

/**
 设置导航栏
 */
- (void)installNavigationBar
{
    self.title = @"随时听";
    //-------------设置导航栏开始--------------------------------
    [super setupNavRightItemsByOne];
    [super setupNavLeftItemsBySearch];
    //-------------设置导航栏结束--------------------------------
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    HMListenAnyTimeResModel *playingModel = [HMAudioPlayerController sharePlayer].playingModel;
    if([HMAudioPlayerController sharePlayer].isPlaying)
    {
        [self.iconImageView startAnimating];
    }
    else
    {
        [self.iconImageView stopAnimating];
    }
    
    if(playingModel)
    {
        [self.lastPlayBtn setHidden:NO];
        self.lastPlayLabel.text = playingModel.resource_name;
    }
    else
    {
        [self.lastPlayBtn setHidden:YES];

    }
    
}
/**
 设置用户欢迎部分
 */
- (void)installUserImageView
{
    // 创建 userImageView
    UIImageView *userImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"default_loading_head"]];
    self.userImageView = userImageView;
    [self.view addSubview:userImageView];
    
    [userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20 + 64);
        make.centerX.equalTo(self.view);
    }];
    
    // 创建 welcomeLabel
    UILabel *welcomeLabel = [[UILabel alloc]init];
    self.welcomeLabel = welcomeLabel;
    [self.view addSubview:welcomeLabel];
    
    [welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userImageView.mas_bottom).offset(15);
        make.centerX.equalTo(self.view);
    }];
    
    // 获取现在时间
    NSDate *date = [NSDate date];
    
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    
    [calender setTimeZone:zone];
    
    NSInteger hour = [calender component:NSCalendarUnitHour fromDate:date];

    welcomeLabel.text = [NSString stringWithFormat:@"%@",hour >= 12?@"下午好":@"上午好"];
    [welcomeLabel sizeToFit];
    
    // 创建 separatorView
    UIView *separatorView = [[UIView alloc]init];
    self.separatorView = separatorView;
    [self.view addSubview:separatorView];
    
    [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(welcomeLabel.mas_bottom).offset(15);
        make.left.offset(35);
        make.right.offset(-35);
        make.height.offset(1.0 / [UIScreen mainScreen].scale);
    }];
    
    separatorView.backgroundColor = [UIColor grayColor];
}

/**
 设置中部文本
 */
- (void)installTextLabels
{
    // 创建 textLabel
    UILabel *textLabel = [[UILabel alloc]init];
    self.textLabel = textLabel;
    [self.view addSubview:textLabel];
    
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.separatorView.mas_bottom).offset(15);
        make.centerX.equalTo(self.view);
    }];
    
    textLabel.text = @"现在, 你想听多长时间 ?";
    textLabel.font = [UIFont systemFontOfSize:20];
    
    // 创建 subTextLabel
    UILabel *subTextLabel = [[UILabel alloc]init];
    self.subTextLabel = subTextLabel;
    [self.view addSubview:subTextLabel];
    
    [subTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textLabel.mas_bottom).offset(15);
        make.centerX.equalTo(self.view);
    }];
    
    subTextLabel.text = @"点选时间, 接下来让得到为你服务";
    subTextLabel.textColor = [UIColor grayColor];
    subTextLabel.font = [UIFont systemFontOfSize:15];
}

/**
 设置下部按钮
 */
- (void)installDailyAudioBtn
{
    // 创建随时听30按钮
    UIButton *dailyAudio30 = [UIButton buttonWithType:0];
    self.dailyAudio30 = dailyAudio30;
    [dailyAudio30 setImage:[UIImage imageNamed:@"dailyAudio30"] forState:UIControlStateNormal];
    [self.view addSubview:dailyAudio30];
    
    [dailyAudio30 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subTextLabel.mas_bottom).offset(10);
        make.width.equalTo(self.view).multipliedBy(0.33);
        make.height.equalTo(dailyAudio30.mas_width);
        make.centerX.equalTo(self.view);
    }];
    
    // 创建随时听60按钮
    UIButton *dailyAudio60 = [UIButton buttonWithType:0];
    self.dailyAudio60 = dailyAudio60;
    [dailyAudio60 setImage:[UIImage imageNamed:@"dailyAudio60"] forState:UIControlStateNormal];
    [self.view addSubview:dailyAudio60];
    
    [dailyAudio60 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dailyAudio30.mas_bottom).offset(0);
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.27);
        make.height.equalTo(dailyAudio60.mas_width);
    }];
    
    // 创建随时听15按钮
    UIButton *dailyAudio15 = [UIButton buttonWithType:0];
    self.dailyAudio15 = dailyAudio15;
    [dailyAudio15 setImage:[UIImage imageNamed:@"dailyAudio15"] forState:UIControlStateNormal];
    [self.view addSubview:dailyAudio15];
    
    [dailyAudio15 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dailyAudio60.mas_left).offset(0);
        make.centerY.equalTo(dailyAudio60);
        make.width.equalTo(self.view).multipliedBy(0.27);
        make.height.equalTo(dailyAudio15.mas_width);
    }];
    
    
    // 创建随时听120按钮
    UIButton *dailyAudio120 = [UIButton buttonWithType:0];
    self.dailyAudio120 = dailyAudio120;
    [dailyAudio120 setImage:[UIImage imageNamed:@"dailyAudio120"] forState:UIControlStateNormal];
    [self.view addSubview:dailyAudio120];
    
    [dailyAudio120 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dailyAudio60.mas_right).offset(0);
        make.centerY.equalTo(dailyAudio60);
        make.width.equalTo(self.view).multipliedBy(0.27);
        make.height.equalTo(dailyAudio120.mas_width);
    }];
    
    // 添加点击事件
    [dailyAudio15 addTarget:self action:@selector(clickdailyAudio:) forControlEvents:UIControlEventTouchDown];
    dailyAudio15.tag = 15;
    [dailyAudio30 addTarget:self action:@selector(clickdailyAudio:) forControlEvents:UIControlEventTouchDown];
    dailyAudio30.tag = 30;
    [dailyAudio60 addTarget:self action:@selector(clickdailyAudio:) forControlEvents:UIControlEventTouchDown];
    dailyAudio60.tag = 60;
    [dailyAudio120 addTarget:self action:@selector(clickdailyAudio:) forControlEvents:UIControlEventTouchDown];
    dailyAudio120.tag = 120;
}

- (void)installLastPlayBtn
{
    // 创建上次播放按钮
    UIButton *lastPlayBtn = [UIButton buttonWithType:0];
    
    [self.view addSubview:lastPlayBtn];
    self.lastPlayBtn = lastPlayBtn;
    [lastPlayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-44);
        make.height.offset(44);
        make.left.right.equalTo(self.view);
    }];
    
    lastPlayBtn.backgroundColor = [UIColor whiteColor];
    // 设置阴影
    lastPlayBtn.layer.cornerRadius = 5;
    lastPlayBtn.layer.shadowOffset =  CGSizeMake(1, 1);
    lastPlayBtn.layer.shadowOpacity = 0.8;
    lastPlayBtn.layer.shadowColor =  [UIColor blackColor].CGColor;
    
    [lastPlayBtn addTarget:self action:@selector(clickLastPlay:) forControlEvents:UIControlEventTouchDown];
    
    // 创建上次播放按钮的icon
    //UIImageView *iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"main_audio_playing_1"]];
    HMMusicImageView *iconImageView = [HMMusicImageView musicImageView];
    self.iconImageView = iconImageView;
    
    [lastPlayBtn addSubview:iconImageView];
    
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(8);
        make.centerY.offset(0);
    }];
    
    // 创建上次播放按钮的描述文字
    UILabel *desLabel = [[UILabel alloc]init];
    
    [lastPlayBtn addSubview:desLabel];
    desLabel.text = @"上次播放:"; // debug
    [desLabel sizeToFit];
    
    [desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(8);
        make.centerY.offset(0);
    }];
    
    desLabel.textColor = [UIColor grayColor];
    desLabel.textAlignment = NSTextAlignmentCenter;
    desLabel.font = [UIFont systemFontOfSize:15];
    
    // 创建上次播放按钮的Title文字
    UILabel *lastPlayLabel = [[UILabel alloc]init];
    [lastPlayBtn addSubview:lastPlayLabel];
    
    lastPlayLabel.textColor = [UIColor grayColor];
    lastPlayLabel.textAlignment = NSTextAlignmentCenter;
    lastPlayLabel.font = [UIFont systemFontOfSize:14];
    lastPlayLabel.text = @"";
    [lastPlayLabel sizeToFit];
    self.lastPlayLabel = lastPlayLabel;
    [lastPlayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(desLabel.mas_right).offset(8);
        make.centerY.offset(0);
        make.right.offset(-16);
    }];
    
    // 创建尾部图片
    UIImageView *tailImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lastAudioRightArrow"]];
    [lastPlayBtn addSubview:tailImageView];
    
    [tailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-8);
        make.centerY.offset(0);
    }];
    
    [self.lastPlayBtn setHidden:YES];
    
    

    
}

#pragma mark - 响应随时听点击事件


- (void)clickdailyAudio:(UIButton *)sender
{
    NSString *timeLength;
    switch (sender.tag) {
        case 15:
        {
            timeLength = @"900";
        }break;
        case 30:
        {
            timeLength = @"1800";
        }break;
        case 60:
        {
            timeLength = @"3600";
        }break;
        case 90:
        {
            timeLength = @"5400";
        }break;
        case 120:
        {
            timeLength = @"7200";
        }break;
            
        default:
        {
            timeLength = @"0";
        }break;
    
    }
    HMListenLoadAudioViewController *loadAudioViewController = [[HMListenLoadAudioViewController alloc]init];
    
    [self mo_presentViewController:loadAudioViewController option:Fall completion:nil];
    
    HMAudioPlayerController *pvc = [HMAudioPlayerController sharePlayer];
    
    [HMListenAnyTimeResViewModel loadListenAnyTimeRes:timeLength andFinished:^(id responseObject) {
        
        loadAudioViewController.finishBlock = ^(UIView *progressView){
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
            // [self disablesAutomaticKeyboardDismissal];
            pvc.hidesBottomBarWhenPushed = YES;
            pvc.modelArray = responseObject;
            [pvc play];
            [self.navigationController pushViewController:pvc animated:YES];
        };
        
        
        loadAudioViewController.modelArrary = responseObject;

    } Failed:^(NSError *error) {
        
    }];
}

- (void)clickLastPlay:(UIButton *)sender
{
    [self.navigationController pushViewController:[HMAudioPlayerController sharePlayer] animated:YES];
}

@end
